using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Shapes;



namespace PlanetWPF.Toolbelt.Controls
{
    /// <summary>
    /// </summary>
    public class PowerGridHeaderCell : PowerGridCell
    {
        private bool _isMouseDown = false;
        private Point _StartPoint;
        private Point _EndPoint;

        static PowerGridHeaderCell()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PowerGridHeaderCell), new FrameworkPropertyMetadata(typeof(PowerGridHeaderCell)));
        }

        protected override void OnPreviewMouseDoubleClick(MouseButtonEventArgs e)
        {
            if ((CellColumn.ColumnType == PowerGridColumnType.Image) || (CellColumn.ColumnType == PowerGridColumnType.RowHeader))
            {
                e.Handled = true;
            }
            else
            {
                ((PowerGridColumnHeaderRow)ParentRow).ColClicked(this, e);
            }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            _isMouseDown = true;
            _StartPoint = e.GetPosition(this);
            base.OnMouseDown(e);
        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            _isMouseDown = false;
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            _EndPoint = e.GetPosition(this);
            if (_isMouseDown)
            {
                _EndPoint = e.GetPosition(this);

            }
            if (_isMouseDown)
            {
                if (Math.Abs(_StartPoint.X - _EndPoint.X) >= 20)
                {
                    MoveColumns(_StartPoint, _EndPoint, this, e);
                }
            }

        }


        protected void MoveColumns(Point startPoint, Point endPoint, object sender, MouseEventArgs e)
        {

            if (startPoint.X < endPoint.X)
            {
                MoveColumnRight();
            }
            else
                if (startPoint.X > endPoint.X)
                {
                    MoveColumnLeft();
                }
            _isMouseDown = false;
        }

        private void MoveColumnRight()
        {
            if (ParentRow != null)
            {
                int ColumnIndex = ParentRow.Columns.IndexOf(CellColumn);
                if (ColumnIndex <= ParentRow.Columns.Count - 1)
                {
                    PowerGridColumnCollection collection = ParentRow.Columns;
                    int newIndex = ColumnIndex + 1;
                    if (newIndex > ParentRow.Columns.Count - 1)
                    {
                        //Set the new index to 1 because 0 holds the row header.
                        newIndex = 1;
                    }
                    collection.Move(ColumnIndex, newIndex);
                }
            }
        }

        private void MoveColumnLeft()
        {
            int ColumnIndex = ParentRow.Columns.IndexOf(CellColumn);
            if (ColumnIndex > 0)
            {
                PowerGridColumnCollection collection = ParentRow.Columns;
                int newIndex = ColumnIndex - 1;
                if (newIndex <= 0)
                {
                    newIndex = ParentRow.Columns.Count - 1;
                }
                collection.Move(ColumnIndex, newIndex);
            }
        }

        protected override void InitializeCell()
        {
            CellValue = CellColumn.DisplayName;
            Width = CellColumn.Width;
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
            }
            base.OnMouseLeftButtonDown(e);
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {

            if (!e.Handled)
            {
                e.Handled = true;
                _isMouseDown = false;
            }
            base.OnMouseLeftButtonUp(e);
        }
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            e.Handled = true;
            base.OnMouseDoubleClick(e);
        }
    }
}
